﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication3
{
    public partial class CustomerRegisteration : Form

    {
          private String connString = Program.ConnStr;
    

        CustomerDataBean customerdatabean = null;
        public CustomerRegisteration()
        {
            InitializeComponent();
        }





        public Boolean validationcontrol()
        {
            Boolean sucess = false;
            if (txtCompanyName.Text.Trim() == null || txtCompanyName.Text.Trim() == "")
            {
                lblStatus.Text = "Enter CompanyName";
                txtCompanyName.Focus();
                txtCompanyName.Text = "";
            }
            else if (txtCompanyCode.Text.Trim() == null || txtCompanyCode.Text.Trim() == "")
            {

                txtCompanyCode.Focus();
                lblStatus.Text = "Enter CompanyCode";
                txtCompanyCode.Text = "";
            }

            else if (cmbLocation.Text.Trim() == null || cmbLocation.Text.Trim() == "")
            {

                cmbLocation.Focus();
                lblStatus.Text = "Enter an valid Location";
                cmbLocation.Text = "";
            }
            else if (rhtAdress.Text.Trim() == null || rhtAdress.Text.Trim() == "")
            {

                rhtAdress.Focus();
                lblStatus.Text = "Enter Adress";
                rhtAdress.Text = "";
            }
            else if (txtContactPerson.Text.Trim() == null || txtContactPerson.Text.Trim()=="")
            {

                txtContactPerson.Focus();
                lblStatus.Text = "Enter a Contact Person";
                txtContactPerson.Text = "";
              
            }
            else if (txtContactNo.Text.Trim() == null || txtContactNo.Text.Trim() == "")
            {

                txtContactNo.Focus();
                lblStatus.Text = "Contact No";
                txtContactNo.Text = "";

            }
            else
            {

                sucess = true;
            }
            return sucess;
        }
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        /// <summary>
        /// when button Cancel Clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
          
            

                
                this.Close();
           
                
            
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (validationcontrol())
            {
                customerdatabean = new CustomerDataBean();
                customerdatabean.Companyname = txtCompanyName.Text.Trim();
                customerdatabean.Custcode = txtCompanyCode.Text.Trim();
                customerdatabean.Location = cmbLocation.Text;
                customerdatabean.Adress = rhtAdress.Text;
                customerdatabean.Contactperson = txtContactPerson.Text;
                customerdatabean.Contactno = txtContactNo.Text;
                customerdatabean.Emailid = txtEmail.Text;
                customerdatabean.Other = rhtOthers.Text;

                if (customerdatabean != null)
                {
                    addaction(customerdatabean);

                }

            }
        }

        public void addaction(CustomerDataBean customerdatabean)
        {

            try
            {

                OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
                oleDbConnection1.Open();
                OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("INSERT INTO customermastertable (companyname, companycode, location, adress, contactperson, contactno, emailid, others) Values ('" + customerdatabean.Companyname + "', '" + customerdatabean.Custcode + "', '" + customerdatabean.Location + "' , '" + customerdatabean.Adress + "'  ,'" + customerdatabean.Contactperson + "','" + customerdatabean.Contactno + "' ,'" + customerdatabean.Emailid + "' ,'" + customerdatabean.Other + "' ) ", oleDbConnection1);
                oleDbCommand1.CommandType = CommandType.Text;
                oleDbCommand1.ExecuteNonQuery();

                oleDbConnection1.Close();
                //  showdataalldriverassignment();
                clearcontrol();
                MessageBox.Show("Sucess");
                CustomerSearch customersearch = new CustomerSearch();
                this.Dispose();
                this.Close();
                customersearch.Show();
               

            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());

            }

        }
        public void clearcontrol()
        {
            txtEmail.Text = "";
            txtCompanyCode.Text = "";
            txtCompanyName.Text = "";
            txtContactNo.Text = "";
            txtContactPerson.Text = "";
             rhtAdress.Text = "";
            rhtOthers.Text = "";
            lblStatus.Text = "";
            cmbLocation.Text = "";

        }

    }
}
